package com.etc.order.provider.mapper;

import com.etc.order.common.domain.OrderMaster;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrderMasterMapper {
    @Select("SELECT * FROM order_master t WHERE t.id = #{orderId}")
    @ResultMap("orderMasterMap")
    OrderMaster getOne(@Param("orderId") String orderId);

    @Select("SELECT * FROM order_master t WHERE t.buyer_name = #{name} ORDER BY update_time DESC LIMIT ${pageSize * pageIndex}, ${pageSize}")
    @ResultMap("orderMasterMap")
    List<OrderMaster> findByBuyerName(@Param("name") String name, @Param("pageIndex") int pageIndex, @Param("pageSize") int pageSize);

    @Insert("INSERT into order_master VALUES(#{orderMaster.orderId}," +
            "#{orderMaster.buyerName}," +
            "#{orderMaster.buyerPhone}," +
            "#{orderMaster.buyerAddress}," +
            "#{orderMaster.orderAmount}," +
            "default," +
            "default," +
            "now()," +
            "now())")
    int createOrder(@Param("orderMaster") OrderMaster orderMaster);

    @Select("SELECT COUNT(*) FROM order_master WHERE buyer_name LIKE CONCAT('%', #{buyerName}, '%')")
    int getTotalCount(String buyerName);

    @Results(id = "orderMasterMap", value = {
            @Result(column = "id", property = "orderId"),
            @Result(column = "buyer_name", property = "buyerName"),
            @Result(column = "buyer_phone", property = "buyerPhone"),
            @Result(column = "buyer_address", property = "buyerAddress"),
            @Result(column = "order_amount", property = "orderAmount"),
            @Result(column = "order_status", property = "orderStatus"),
            @Result(column = "pay_status", property = "payStatus"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
    })
    @Select("SELECT * FROM order_master ORDER BY update_time DESC LIMIT ${pageSize * pageIndex}, ${pageSize}")
    List<OrderMaster> findAll(@Param("pageIndex") int pageIndex, @Param("pageSize") int pageSize);

    @Update("UPDATE order_master SET buyer_name = #{orderMaster.buyerName}, " +
            "buyer_phone = #{orderMaster.buyerPhone}, " +
            "buyer_address = #{orderMaster.buyerAddress}, " +
            "order_amount = #{orderMaster.orderAmount}, " +
            "order_status = #{orderMaster.orderStatus}, " +
            "pay_status = #{orderMaster.payStatus}, " +
            "create_time = #{orderMaster.createTime}, " +
            "update_time = #{orderMaster.updateTime} WHERE id = #{orderMaster.orderId}"
    )
    int update(@Param("orderMaster") OrderMaster orderMaster);
}
